<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="楼栋" prop="buildingType">
        <el-select v-model="queryParams.buildingType" placeholder="请选择楼栋" clearable>
          <el-option
            v-for="dict in dict.type.building_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="楼层" prop="floor">
        <el-input
          v-model="queryParams.floor"
          placeholder="请输入楼层"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间号" prop="roomNumber">
        <el-input
          v-model="queryParams.roomNumber"
          placeholder="请输入房间号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间类型）" prop="roomType">
        <el-select v-model="queryParams.roomType" placeholder="请选择房间类型）" clearable>
          <el-option
            v-for="dict in dict.type.room_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="房间容纳人数" prop="capacity">
        <el-input
          v-model="queryParams.capacity"
          placeholder="请输入房间容纳人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="每晚价格" prop="pricePerNight">
        <el-input
          v-model="queryParams.pricePerNight"
          placeholder="请输入每晚价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间状态）" prop="roomStatus">
        <el-select v-model="queryParams.roomStatus" placeholder="请选择房间状态）" clearable>
          <el-option
            v-for="dict in dict.type.room_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户身份证" prop="userIdCard">
        <el-input
          v-model="queryParams.userIdCard"
          placeholder="请输入用户身份证"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户入住日期" prop="userCheckinDate">
        <el-date-picker clearable
                        v-model="queryParams.userCheckinDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择用户入住日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hotefl:rooms:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hotefl:rooms:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hotefl:rooms:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hotefl:rooms:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="preview(0)"
        >预览A栋情况
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="preview(1)"
        >预览B栋情况
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="preview(2)"
        >预览C栋情况
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="roomsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="房间ID，主键，自增" align="center" prop="roomId"/>
      <el-table-column label="楼栋" align="center" prop="buildingType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.building_type" :value="scope.row.buildingType"/>
        </template>
      </el-table-column>
      <el-table-column label="楼层" align="center" prop="floor"/>
      <el-table-column label="房间号" align="center" prop="roomNumber"/>
      <el-table-column label="房间类型）" align="center" prop="roomType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.room_type" :value="scope.row.roomType"/>
        </template>
      </el-table-column>
      <el-table-column label="房间容纳人数" align="center" prop="capacity"/>
      <el-table-column label="每晚价格" align="center" prop="pricePerNight"/>
      <el-table-column label="房间设施" align="center" prop="amenities"/>
      <el-table-column label="房间图片" align="center" prop="imgUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imgUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="房间状态）" align="center" prop="roomStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.room_status" :value="scope.row.roomStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="用户姓名" align="center" prop="userName"/>
      <el-table-column label="用户身份证" align="center" prop="userIdCard"/>
      <el-table-column label="用户入住日期" align="center" prop="userCheckinDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.userCheckinDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hotefl:rooms:edit']"
          >修改
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleCleanRoom(scope.row)"
          >一键清理房间
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="toCheckout(scope.row)"
          >收款
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hotefl:rooms:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改房间管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="楼栋" prop="buildingType">
          <el-select v-model="form.buildingType" placeholder="请选择楼栋">
            <el-option
              v-for="dict in dict.type.building_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼层" prop="floor">
          <el-input v-model="form.floor" placeholder="请输入楼层"/>
        </el-form-item>
        <el-form-item label="房间号" prop="roomNumber">
          <el-input v-model="form.roomNumber" placeholder="请输入房间号"/>
        </el-form-item>
        <el-form-item label="房间类型）" prop="roomType">
          <el-select v-model="form.roomType" placeholder="请选择房间类型）">
            <el-option
              v-for="dict in dict.type.room_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房间容纳人数" prop="capacity">
          <el-input v-model="form.capacity" placeholder="请输入房间容纳人数"/>
        </el-form-item>
        <el-form-item label="每晚价格" prop="pricePerNight">
          <el-input v-model="form.pricePerNight" placeholder="请输入每晚价格"/>
        </el-form-item>
        <el-form-item label="房间设施" prop="amenities">
          <el-input v-model="form.amenities" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="房间图片" prop="imgUrl">
          <image-upload v-model="form.imgUrl"/>
        </el-form-item>
        <el-form-item label="房间状态）" prop="roomStatus">
          <el-radio-group v-model="form.roomStatus">
            <el-radio
              v-for="dict in dict.type.room_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id"/>
        </el-form-item>
        <el-form-item label="用户姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户姓名"/>
        </el-form-item>
        <el-form-item label="用户身份证" prop="userIdCard">
          <el-input v-model="form.userIdCard" placeholder="请输入用户身份证"/>
        </el-form-item>
        <el-form-item label="用户入住日期" prop="userCheckinDate">
          <el-date-picker clearable
                          v-model="form.userCheckinDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择用户入住日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="checkout.title" :visible.sync="checkout.open" width="500px" append-to-body>
      <!--支付宝收款码-->
      <div style="text-align: center">请支付{{ checkout.money }}元</div>
      <image-preview :src="checkout.imgUrl" :width="50" :height="50"/>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="toPay(checkout.id)">确 定</el-button>
        <el-button @click="checkout.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!--// 全屏-->
    <el-dialog :title="pv.id" :visible.sync="pv.open" :fullscreen="true" append-to-body>
      <el-row v-for="(lc, index) in 10" :key="index">
        <el-col span="2" v-for="(item, index) in roomsList.filter((item) => { return item.floor === lc})">
          <el-card class="box-card" v-if="item.roomStatus === 0" style="background-color: rgba(12,229,238,0.5)">
            <div slot="header" class="clearfix">
              <span>{{ lc }}楼 </span>
              <el-button style="float: right; padding: 3px 0" type="text"></el-button>
            </div>
            <div class="text item">
              <!--显示入住状态-->
              <span>可入住 房间号为 {{ item.roomNumber }}</span>
            </div>
          </el-card>
          <el-card class="box-card" v-if="item.roomStatus === 1" style="background-color: rgba(173,10,238,0.5)">
            <div slot="header" class="clearfix">
              <span>{{ lc }}楼 </span>
              <el-button style="float: right; padding: 3px 0" type="text"></el-button>
            </div>
            <div class="text item">
              <!--显示入住状态-->
              <span>已入住 </span>
              <h>房间号为 {{ item.roomNumber }}</h>
            </div>
          </el-card>

        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {listRooms, getRooms, delRooms, addRooms, updateRooms} from "@/api/hotefl/rooms";

export default {
  name: "Rooms",
  dicts: ['building_type', 'room_type', 'room_status'],
  data() {
    return {
      // 预览
      pv: {
        id: '',
        open: false
      },
      // checkout
      checkout: {
        money: 0,
        id: '',
        open: false,
        title: "扫码付款",
        imgUrl: this.$store.getters.avatar,
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房间管理表格数据
      roomsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        buildingType: null,
        floor: null,
        roomNumber: null,
        roomType: null,
        capacity: null,
        pricePerNight: null,
        amenities: null,
        imgUrl: null,
        roomStatus: null,
        userId: null,
        userName: null,
        userIdCard: null,
        userCheckinDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        buildingType: [
          {required: true, message: "楼栋不能为空", trigger: "change"}
        ],
        floor: [
          {required: true, message: "楼层不能为空", trigger: "blur"}
        ],
        roomNumber: [
          {required: true, message: "房间号不能为空", trigger: "blur"}
        ],
        roomType: [
          {required: true, message: "房间类型）不能为空", trigger: "change"}
        ],
        capacity: [
          {required: true, message: "房间容纳人数不能为空", trigger: "blur"}
        ],
        pricePerNight: [
          {required: true, message: "每晚价格不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    preview(typeId) {
      this.pv.id = typeId;
      listRooms(this.queryParams).then(response => {
        this.queryParams.buildingType = typeId;
        this.queryParams.pageSize = 20;
        this.roomsList = response.rows;
        this.total = response.total;
        this.pv.open = true;
      });
    },
    /** 查询房间管理列表 */
    getList() {
      this.loading = true;
      listRooms(this.queryParams).then(response => {
        this.roomsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        roomId: null,
        buildingType: null,
        floor: null,
        roomNumber: null,
        roomType: null,
        capacity: null,
        pricePerNight: null,
        amenities: null,
        imgUrl: null,
        roomStatus: null,
        userId: null,
        userName: null,
        userIdCard: null,
        userCheckinDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.roomId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加房间管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const roomId = row.roomId || this.ids
      getRooms(roomId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改房间管理";
      });
    },
// 一键清理房间
    handleCleanRoom(row) {
      this.reset();
      const roomId = row.roomId || this.ids
      let data;
      getRooms(roomId).then(response => {
        data = response.data;
        // 状态默认可用
        data.roomStatus = 0;
        // 这里默认填写写管理员id
        data.userId = 0;
        data.userName = " ";
        data.userIdCard = " ";
        updateRooms(data).then(response => {
          this.$modal.msgSuccess("");
          this.getList();
        });
      });
    },
    toCheckout(row) {
      const roomId = row.roomId || this.ids
      getRooms(roomId).then(response => {
        this.form = response.data;
        this.checkout.open = true;
        this.checkout.id = roomId;
        this.checkout.money = roomId;
        this.checkout.title = "扫描支付";
        // 计算金额 当前时间- 入住时间 * 房间价格
        let number = new Date().getTime() - new Date(this.form.userCheckinDate).getTime();
        // 转天数
        number = number / (1000 * 60 * 60 * 24);
        // 保留一位小数
        this.checkout.money = (number * this.form.pricePerNight).toFixed(1);
      });
      this.reset();
    },
    toPay(roomId) {
      getRooms(roomId).then(response => {
        this.form = response.data;
        // 状态默认可用
        this.form.roomStatus = 0;
        // 这里默认填写写管理员id
        this.form.userId = null;
        this.form.userName = "等待阿姨打扫";
        this.form.userIdCard = "等待阿姨打扫";
        this.form.userCheckinDate = null;
        updateRooms(this.form).then(response => {
          this.$modal.msgSuccess("支付成功");
          this.getList();
          this.checkout.open = false;
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.roomId != null) {
            updateRooms(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            // 状态默认可用
            this.form.roomStatus = 0;
            // 这里默认填写写管理员id
            this.form.userId = null;
            this.form.userName = "房间创建成功,请打扫";
            this.form.userIdCard = "房间创建成功,请打扫";
            addRooms(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const roomIds = row.roomId || this.ids;
      this.$modal.confirm('是否确认删除房间管理编号为"' + roomIds + '"的数据项？').then(function () {
        return delRooms(roomIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hotefl/rooms/export', {
        ...this.queryParams
      }, `rooms_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
